package com.saasitems.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.saasitems.admin.dao.*;
import com.saasitems.admin.model.*;
import com.saasitems.admin.web.vo.orderinfo.OrderInfoQueryVO;
import com.saasitems.admin.web.vo.orderinfo.AddOrderInfoVO;
import com.saasitems.admin.web.vo.orderinfo.OrderInfoVO;
import com.saasitems.admin.web.vo.product.ProductVO;
import com.saasitems.common.model.PageInfo;
import com.saasitems.common.utils.DateUtils;
import com.saasitems.common.utils.SequenceUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by Administrator on 2016/12/26.
 */
@Service
public class OrderInfoService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private OrderProductMapper orderProductMapper;

    @Autowired
    private PayLogMapper payLogMapper;

    @Autowired
    private SequenceMapper sequenceMapper;

    public Object insert(AddOrderInfoVO vo) {
        if (vo.getItem() == null) {
            return null;
        }
        if (vo.getItem().getProductId() == null) {
            return null;
        }
        if (vo.getItem().getProductCycle() == null) {
            return null;
        }
        if (vo.getItem().getProductPrice() == null) {
            return null;
        }

        Product p = this.productMapper.selectByPrimaryKey(vo.getItem().getProductId());
        if (vo.getItem().getProductCycle().equals(2)) {
            // 年
            if (p.getYyPrice().compareTo(vo.getItem().getProductPrice()) != 0) {
                return null;
            }
        } else {
            // 月
            if (p.getMnPrice().compareTo(vo.getItem().getProductPrice()) != 0) {
                return null;
            }
        }

        Map<String, Object> map = new HashMap<String, Object>();
        sequenceMapper.getSequence(map);
        Integer seq = null;
        if (map.containsKey("orderseq")) {
            seq = (Integer) map.get("orderseq");
        }

        BigDecimal amount = null;
        if (vo.getItem().getProductPrice() != null) {
            amount = vo.getItem().getProductPrice().multiply(new BigDecimal(vo.getItem().getProductNum()));
        } else {
            amount = new BigDecimal(0.0);
        }

        OrderInfo oi = new OrderInfo();
        oi.setUserId(vo.getUserId());
        oi.setOrderSn(SequenceUtils.format(seq));
        oi.setOrderStatus(0);
        oi.setPayStatus(0);
        oi.setReturnedStatus(0);
        oi.setOrderAmount(amount);
        oi.setAddTime(new Date());
        orderInfoMapper.insert(oi);

        OrderProduct op = new OrderProduct();

        op.setOrderId(oi.getOrderId());
        op.setProductId(p.getId());
        op.setProductSn(p.getSn());
        op.setProductName(p.getName());

        // 是否试用
        op.setIsTryout(vo.getItem().getIsTryout());
        // 选择的价格
        op.setProductPrice(vo.getItem().getProductPrice());
        op.setProductNum(vo.getItem().getProductNum());
        // 选择的周期
        op.setProductCycle(vo.getItem().getProductCycle());

        long days = 0;
        // 试用
        if (vo.getItem().getIsTryout().equals(1)) {
            days = p.getTryoutDay();
        } else if (vo.getItem().getProductCycle().equals(1)) {
            days = vo.getItem().getProductNum() * 30;
        } else if (vo.getItem().getProductCycle().equals(2)) {
            days = vo.getItem().getProductNum() * 365;
        }

        Date startTime = new Date();
        // yyyy-MM-dd
        String strTime = DateUtils.formatDate(startTime);

        op.setStartTime(DateUtils.parseDate(strTime));
        op.setEndTime(DateUtils.getAfterDate(strTime, days));

        op.setCommentId(0L);
        orderProductMapper.insert(op);
        return "";
    }

    public List<OrderInfoVO> query(OrderInfoQueryVO queryVO, PageInfo page) {
        OrderInfoExample ex = new OrderInfoExample();
        OrderInfoExample.Criteria c = ex.createCriteria();
        if (queryVO != null) {
            if (StringUtils.isNotBlank(queryVO.getOrderSn())) {
                c.andOrderSnEqualTo(queryVO.getOrderSn());
            }
//            if (StringUtils.isNotBlank(queryVO.getName())) {
//                c.andSnEqualTo(queryVO.getName());
//            }
            if (queryVO.getTimeStart() != null) {
                c.andAddTimeGreaterThanOrEqualTo(queryVO.getTimeStart());
            }
            if (queryVO.getTimeEnd() != null) {
                c.andAddTimeLessThanOrEqualTo(queryVO.getTimeEnd());
            }
        }

        if (page != null) {
            PageHelper.startPage(page.getPageNo(), page.getPageSize());
        }

        List<OrderInfo> list = this.orderInfoMapper.selectByExample(ex);
        if (page != null) {
            // 取count的值
            page.setTotalCount((long)((Page) list).getTotal());
        }
        List<OrderInfoVO> ret = new ArrayList<OrderInfoVO>();
        List<Long> productIds = new ArrayList<Long>();
        for (OrderInfo oi : list) {
            OrderInfoVO vo = new OrderInfoVO();
            BeanUtils.copyProperties(oi, vo);

            // ProductExample pex = new ProductExample();
            // pex.createCriteria().andIdIn(productIds);
            // List<Product> plist = this.productMapper.selectByExample(pex);
            // Product p = this.productMapper.selectByPrimaryKey(p.get)

            ret.add(vo);
        }
        return ret;
    }
}
